package com.base.base
import android.app.Activity
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView

/**
 * RecyclerView数据列表适配器
 * @param dataList 数据集
 * @param activity Activity 如果需要在布局器中回调到指定Activity可以配置
 * @param options 一个预留无类型类 可以根据需要配置各种函数  不过需要在使用时强转类型
 */
abstract class IAdapter (var dataList: MutableList<out Any>, var activity: Activity? = null, var options: Any? = null) : RecyclerView.Adapter<IHolder>(){

    constructor(): this(mutableListOf())
    constructor(dataList: MutableList<out Any>): this(dataList,null)
    constructor(dataList: MutableList<out Any>,activity: Activity?): this(dataList,activity,null)

    /**
     * 创建Holder
     */
    final override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): IHolder {
        return createHolder(parent,viewType)
    }

    /**
     * 创建Holder
     */
    abstract fun createHolder(parent: ViewGroup, viewType: Int): IHolder

    /**
     * 带动画删除Item 子类可以通过该方法回调到自身适配器进行删除对应数据的操作
     */
    open fun remove(holder: IHolder){
        if(holder.adapterPosition < dataList.size){
            dataList.removeAt(holder.adapterPosition)
            notifyItemRemoved(holder.adapterPosition)
        }
    }

    /**
     * 绑定Holder
     */
    final override fun onBindViewHolder(holder: IHolder, position: Int, payloads: MutableList<Any>) {
        super.onBindViewHolder(holder, position, payloads)
    }

    /**
     * 绑定Holder
     */
    final override fun onBindViewHolder(holder: IHolder, position: Int) {
        bindHolder(holder,position)
    }

    /**
     * 绑定Holder
     */
    open fun bindHolder(holder: IHolder, position: Int){
        holder.bindData(dataList.getOrNull(position))
    }

    /**
     * 获取Item数量
     */
    override fun getItemCount(): Int {
        return dataList.size
    }



}
